package game;

import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;

@SuppressWarnings("serial")
public class ScoreList extends JFrame {

	/**
	 * @param args
	 */
	ArrayList<Player> players;
//	Player me;
	private ArrayList<JLabel> labels = new ArrayList<JLabel>();
	private static ScoreList scoreList;
	/**
	 * 
	 * @param players
	 */
	public static ScoreList getInstance(){
		if(scoreList==null){
			scoreList = new ScoreList();
		}
		return scoreList;
	}
	
	private ScoreList() {
		super("TKgame v. 1.0");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocation(600,100);
		this.setSize(100, 500);
		this.setResizable(true);
		this.setLayout(new GridLayout(20, 20, 0, 0));
		this.setVisible(true);
		this.players = new ArrayList<Player>();
		draw();
		this.setAlwaysOnTop(false);
	}
	
	/**
	 * 
	 */
	public void draw() {
		for (int j = 0; j < players.size(); j++) {
			JLabel l = new JLabel(players.get(j).toString());
			l.setSize(50, 200);
			this.add(l);
			labels.add(l);
		}	
	}
	
	/**
	 * 
	 * @param p
	 */
	public void updateScoreOnScreen(Player p) {
		int playerno = players.indexOf(p);
		labels.get((playerno)).setText(players.get(playerno).toString());
	}
	
	public void updateAll(){
		for(int i=0; labels.size()>i;i++){
			labels.get(i).setText(players.get(i).toString());
		}
	}
	
	public void addToScoreList(Player p){
		players.add(p);
		JLabel l = new JLabel(p.toString());
		l.setSize(50, 200);
		this.add(l);
		labels.add(l);
		updateAll();
	}
	
	public void removeFromScoreList(Player p){
		players.remove(p);
		updateAll();
	}
}	
	
